<?php
declare(strict_types=1);

namespace App\Common\Modular\System\Serve;

use App\Common\Modular\System\Model\SmsModel;
use Core\Basics\BasicsServe;
use Zms\Sms\Sms;
use Zms\Sms\SmsResult;
use Zms\Unit\Lists;
use Zms\Unit\Str;

/**
 * 短信日志
 */
class SmsServe extends BasicsServe
{
    /**
     * @return SmsModel::class
     */
    public static function model(): string
    {
        return SmsModel::class;
    }


    /**
     * 获取短信驱动和服务商
     * @return array
     */
    public function drives(): array
    {
        $drives = [];
        foreach (Sms::drives() as $item) {
            $names = Str::explode('\\', $item['drive']);
            $drives[] = [...$item, 'sign' => Lists::last($names)];
        }
        return $drives;
    }


    /**
     * 添加短信发送记录
     * @param SmsResult $sendResult
     * @return void
     */
    public function addLog(SmsResult $sendResult): void
    {
        $this->created(
            [
                'template_id' => $sendResult->template_key,
                'driver' => $sendResult->driver,
                'phone' => $sendResult->phone,
                'content' => $sendResult->content,
                'status' => $sendResult->status ? 1 : 0,
            ]
        );
    }
}